home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet multimedia / Animacje, filmy i prezentacje / Modelowanie 3D / K-3D 0.6.5.0 / k3d-all-in-one-setup-0.6.5.0.exe / k3d-setup-0.6.5.0.exe / share / shaders / k3d_project.h < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-19  |  4.5 KB  |  187 lines

  1. /************************************************************************
  2.  * project.h - Routines for texture projection
  3.  *
  4.  * Author: Larry Gritz (gritzl@acm.org)
  5.  *
  6.  * $Revision: 1.1 $    $Date: 2004/05/19 18:15:20 $
  7.  *
  8.  ************************************************************************/
  9.  
  10.  
  11.  
  12. #ifndef PROJECT_H
  13. #define PROJECT_H 1
  14.  
  15.  
  16. #ifndef FILTERWIDTH_H
  17. #include "k3d_filterwidth.h"
  18. #endif
  19.  
  20.  
  21. /* Project 3-D points onto a unit sphere centered at the origin */
  22. void
  23. spherical_projection (point p; output float ss, tt, ds, dt;)
  24. {
  25.     extern float du, dv;    /* Used by the filterwidth macro */
  26.     vector V = normalize(vector p);
  27.     ss = (-atan (ycomp(V), xcomp(V)) + PI) / (2*PI);
  28.     tt = 0.5 - acos(zcomp(V)) / PI;
  29.     ds = filterwidth (ss);
  30.     if (ds > 0.5)
  31.     ds = max (1-ds, MINFILTWIDTH);
  32.     dt = filterwidth (tt);
  33.     if (dt > 0.5)
  34.     dt = max (1-dt, MINFILTWIDTH);
  35. }
  36.     
  37.  
  38.  
  39. /* Project 3-D points onto a cylinder about the z-axis between z=0 and z=1 */
  40. void
  41. cylindrical_projection (point p; output float ss, tt, ds, dt;)
  42. {
  43.     extern float du, dv;    /* Used by the filterwidth macro */
  44.     vector V = normalize(vector p);
  45.     ss = (-atan (ycomp(V), xcomp(V)) + PI) / (2*PI);
  46.     tt = zcomp(p);
  47.     ds = filterwidth (ss);
  48.     if (ds > 0.5)
  49.     ds = max (1-ds, MINFILTWIDTH);
  50.     dt = filterwidth (tt);
  51. }
  52.  
  53.  
  54.  
  55.  
  56. void
  57. ProjectTo2D (string projection;
  58.          point P; string whichspace;
  59.          matrix xform;
  60.          output float ss, tt;
  61.          output float ds, dt; )
  62. {
  63.     point Pproj;
  64.     extern float du, dv;    /* Used by the filterwidth macro */
  65.     if (projection == "st") {
  66.     extern float s, t;
  67.     Pproj = point (s, t, 0);
  68.     } else {
  69.         Pproj = transform (whichspace, P);
  70.     }
  71. #pragma nolint 2
  72.     Pproj = transform (xform, Pproj);
  73.     if (projection == "planar" || projection == "st") {
  74.     ss = xcomp(Pproj);
  75.     tt = ycomp(Pproj);
  76.     ds = filterwidth (ss);
  77.     dt = filterwidth (tt);
  78.     }
  79.     else if (projection == "perspective") {
  80.     float z = max (zcomp(Pproj), 1.0e-6);  /* avoid zero division */
  81.     ss = xcomp(Pproj) / z;
  82.     tt = ycomp(Pproj) / z;
  83.     ds = filterwidth (ss);
  84.     dt = filterwidth (tt);
  85.     }
  86.     /* Special cases for the projections that may wrap */
  87.     else if (projection == "spherical")
  88.     spherical_projection (Pproj, ss, tt, ds, dt);
  89.     else if (projection == "cylindrical")
  90.     cylindrical_projection (Pproj, ss, tt, ds, dt);
  91. }
  92.  
  93.  
  94.  
  95. color
  96. GetColorTextureAndAlpha (string texturename;
  97.              string projection;
  98.              point P; string whichspace;
  99.              matrix xform;
  100.              float blur;
  101.              uniform float alphachannel;
  102.              output float alpha;
  103.     )
  104. {
  105.     float ss, tt, ds, dt;
  106.     ProjectTo2D (projection, P, whichspace, xform, ss, tt, ds, dt);
  107.     ds *= 0.5;  dt *= 0.5;
  108.     color Ct = color texture (texturename, ss-ds, tt-dt, ss+ds, tt-dt,
  109.                   ss-ds, tt+dt, ss+ds, tt+dt, "blur", blur);
  110.     alpha = float texture (texturename[alphachannel], ss-ds, tt-dt,
  111.                ss+ds, tt-dt, ss-ds, tt+dt, ss+ds, tt+dt,
  112.                "blur", blur, "fill", 1);
  113.     return Ct;
  114. }
  115.  
  116.  
  117.  
  118. float
  119. GetFloatTextureAndAlpha (string texturename;
  120.              string projection;
  121.              point P; string whichspace;
  122.              matrix xform;
  123.              float blur;
  124.              uniform float alphachannel;
  125.              output float alpha;
  126.     )
  127. {
  128.     float ss, tt, ds, dt;
  129.     ProjectTo2D (projection, P, whichspace, xform, ss, tt, ds, dt);
  130.     ds *= 0.5;  dt *= 0.5;
  131.     float val = float texture (texturename, ss-ds, tt-dt, ss+ds, tt-dt,
  132.                    ss-ds, tt+dt, ss+ds, tt+dt, "blur", blur);
  133.     alpha = float texture (texturename[alphachannel], ss-ds, tt-dt,
  134.                ss+ds, tt-dt, ss-ds, tt+dt, ss+ds, tt+dt,
  135.                "blur", blur, "fill", 1);
  136.     return val;
  137. }
  138.  
  139.  
  140.  
  141. color
  142. ApplyColorTextureOver (color basecolor;
  143.                string texturename;
  144.                string projection;
  145.                point P; string whichspace;
  146.                matrix xform;
  147.                float blur;
  148.     )
  149. {
  150.     float alpha;
  151.     color Ct = GetColorTextureAndAlpha (texturename, projection, P, whichspace,
  152.                     xform, blur, 3, alpha);
  153.     return Ct + (1-alpha)*basecolor;
  154. }
  155.  
  156.  
  157.  
  158. float
  159. ApplyFloatTextureOver (float baseval;
  160.                string texturename;
  161.                string projection;
  162.                point P; string whichspace;
  163.                matrix xform;
  164.                float blur;
  165.     )
  166. {
  167.     float alpha;
  168.     float x = GetFloatTextureAndAlpha (texturename, projection, P, whichspace,
  169.                     xform, blur, 3, alpha);
  170.     return x + (1-alpha)*baseval;
  171. }
  172.  
  173.  
  174. /* Helper function to convert arrays of 16 floats to matrices */
  175. matrix array_to_mx (float mx[16])
  176. {
  177.     return matrix (mx[0], mx[1], mx[2], mx[3],
  178.            mx[4], mx[5], mx[6], mx[7],
  179.            mx[8], mx[9], mx[10], mx[11],
  180.            mx[12], mx[13], mx[14], mx[15]);
  181. }
  182.  
  183.  
  184.  
  185.  
  186. #endif /* PROJECT_H */
  187.